//
//  TListZBZYCell.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/4/6.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit
 
import Kingfisher
class TListZBZYCell: TenderBaseCell {
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
    
        
        
    }
    /// 三种模式   投标样式  销售招标带图标样式    销售招标不带图样式  --- withPicture 只有销售招标用到
    
    func makeConstrainsts(_ listType:TBandJPType = .TB,withPicture:Bool = true) {
        if listType == .TB {
            dingwei.isHidden = false
            btn1.snp.makeConstraints { (make) in
                make.left.equalTo(lab1)
                make.width.equalTo(60)
                make.height.equalTo(25)
                make.top.equalTo(lab1)
            }
            lab1.snp.makeConstraints { (make) in
                make.left.equalTo(self.contentView).offset(11)
                make.right.equalTo(self.contentView).offset(-15)
                make.top.equalTo(self.contentView).offset(6)
            }
            
            
            lab2.snp.makeConstraints { (make) in
                make.left.right.equalTo(lab1)
                make.top.equalTo(lab1.snp.bottom).offset(6)
            }
            
            dingwei.snp.makeConstraints { (make) in
                make.height.equalTo(15)
                make.width.equalTo(14)
                make.left.equalTo(lab1)
                make.top.equalTo(lab2.snp.bottom).offset(6)
            }
            
            lab3.snp.makeConstraints { (make) in
                make.left.equalTo(lab1)
                make.right.equalTo(self.contentView).offset(10)
                make.top.equalTo(dingwei)
            }
            
            lab4.snp.makeConstraints { (make) in
                make.left.equalTo(lab1)
                make.right.equalTo(oper_btn.snp.left).offset(-2)
                make.bottom.equalTo(self.contentView).offset(-10)
            }
            
            oper_btn.snp.makeConstraints { (make) in
                make.top.equalTo(lab3.snp.bottom).offset(2)
                make.width.equalTo(70)
                make.right.equalTo(self.contentView).offset(-15)
                make.height.equalTo(32)
                make.bottom.equalTo(self.contentView).offset(-5)
                
            }
        } else { // 竞拍
            dingwei.isHidden = true
            if withPicture {
                imageV.snp.makeConstraints { (make) in
                    make.left.equalTo(self.contentView).offset(12)
                    make.centerY.equalTo(self.contentView)
                    make.width.height.equalTo(95)
                }
                lab1.snp.makeConstraints { (make) in
                    make.left.equalTo(imageV.snp.right).offset(12)
                    make.right.equalTo(self.contentView).offset(-10)
                    make.top.equalTo(self.contentView).offset(12)
                    //                make.bottom.equalTo(self).offset(-100)
                }
                btn1.snp.makeConstraints { (make) in
                    make.left.equalTo(lab1)
                    make.width.equalTo(60)
                    make.height.equalTo(25)
                    make.top.equalTo(lab1.snp.bottom).offset(3)
                }
                chujiaLab.snp.makeConstraints { (make) in
                    make.centerY.equalTo(btn1)
                    make.right.equalTo(self.contentView).offset(-10)
                }
                
                lab2.snp.makeConstraints { (make) in
                    make.left.right.equalTo(lab1)
                    make.top.equalTo(btn1.snp.bottom).offset(3)
                }
                lab3.snp.makeConstraints { (make) in
                    make.left.right.equalTo(lab1)
                    make.top.equalTo(lab2.snp.bottom).offset(3)
                }
                
                lab4.snp.makeConstraints { (make) in
                    make.left.right.equalTo(lab1)
                    make.top.equalTo(lab3.snp.bottom).offset(3)
                    
                }
                oper_btn.snp.makeConstraints { (make) in
                    make.top.equalTo(lab4.snp.bottom).offset(2)
                    make.width.equalTo(70)
                    make.right.equalTo(self.contentView).offset(-15)
                    make.height.equalTo(32)
                    make.bottom.equalTo(self.contentView).offset(-5)
                }
            } else {
                
                lab1.snp.makeConstraints { (make) in
                    make.left.equalTo(self.contentView).offset(12)
                    make.right.equalTo(self.contentView).offset(-10)
                    make.top.equalTo(self.contentView).offset(12)
                    //                make.bottom.equalTo(self).offset(-100)
                }
                btn1.snp.makeConstraints { (make) in
                    make.left.equalTo(lab1)
                    make.width.equalTo(60)
                    make.height.equalTo(25)
                    make.top.equalTo(lab1.snp.bottom).offset(3)
                }
                chujiaLab.snp.makeConstraints { (make) in
                    make.centerY.equalTo(btn1)
                    make.right.equalTo(self.contentView).offset(-10)
                }
                
                lab2.snp.makeConstraints { (make) in
                    make.left.right.equalTo(lab1)
                    make.top.equalTo(btn1.snp.bottom).offset(3)
                }
                lab3.snp.makeConstraints { (make) in
                    make.left.right.equalTo(lab1)
                    make.top.equalTo(lab2.snp.bottom).offset(3)
                }
                
                lab4.snp.makeConstraints { (make) in
                    make.left.right.equalTo(lab1)
                    make.top.equalTo(lab3.snp.bottom).offset(3)
                    
                }
                oper_btn.snp.makeConstraints { (make) in
                    make.top.equalTo(lab4.snp.bottom).offset(2)
                    make.width.equalTo(70)
                    make.right.equalTo(self.contentView).offset(-15)
                    make.height.equalTo(32)
                    make.bottom.equalTo(self.contentView).offset(-5)
                }
            }
            
        }
        
        
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var one = ZtbContentModel(JSON:["":""])! {
        didSet {
            if one.innerType! == .tbRecordKHW || one.innerType! == .chujiaRecordKHW {
                lab1.attributedText = NSAttributedString(string: "                \(one.zTbCompany.unwrap)", attributes: titleAttributes)
                
                var  lab2Str:String = ""
                if one.innerType! == .tbRecordKHW {
                    let cont = one.zContent.unwrap.isEmpty ? "" :"\n投标说明:\(one.zContent.unwrap)"
                    
                    
                    lab2Str = "出价:\(one.zPrice.unwrap)元\n联系人:\(one.zName.unwrap)     联系电话:\(one.zTel.unwrap)    \(cont)"
                } else {
                    lab2Str = "出价：\(one.zPrice.unwrap)元   \(one.zBidNum.unwrap)头   联系人：\(one.zName.unwrap) 电话:\(one.zTel.unwrap)"
                }
                lab2.attributedText = NSAttributedString(string: lab2Str, attributes: lab3Attributes)
                
                
                lab3.text = "    \(one.zCity.unwrap)    \(one.zCreateTm.unwrap)"
                
                oper_btn.setTitle("", for: .normal)
                btn1.setZBJPStatus(listType:.TB,auditmark: intTran(one.zAuditMark), status: intTran(one.zStatus),tbType: one.innerType!)
                lab4.text = one.zPriceRank.unwrap
            } else if one.innerType! == .chujiaRecordJP {
                lab1.attributedText = NSAttributedString(string: "                \(one.zTbCompany.unwrap)", attributes: titleAttributes)
                
                var  lab2Str:String = ""
               
                    lab2Str = "联系人：\(one.zName.unwrap)"
               
                lab2.attributedText = NSAttributedString(string: lab2Str, attributes: lab3Attributes)
 
                lab3.text = ""
                dingwei.isHidden = true
                oper_btn.setTitle("", for: .normal)
                btn1.setZBJPStatus(listType:.TB,auditmark: intTran(one.zAuditMark), status: intTran(one.zStatus),tbType: one.innerType!)
                lab4.text = one.zPriceRank.unwrap
            } else if one.innerType! == .jpListKHW || one.innerType! == .jpMyjpKHW || one.innerType! == .jpMyMarkKHW || one.innerType! == .jpInfo_oaOA || one.innerType! == .jpInfo_followTagOA {
                
                lab1.text = one.zTitle
                let real_status = one.zStatus.isSome ? intTran(one.zStatus) : intTran(one.zZbStatus)
                let btnName =  btn1.setZBJPStatus(listType:.JP,auditmark: intTran(one.zAuditMark), status: real_status,tbType: one.innerType!)
                let ssst = btnName == "未开始" ? "\(one.zBeginDate.unwrap)开始" : "\(one.zEndDate.unwrap)结束"
                lab2.text =  "\(ssst)   \(one.zCity.unwrap)"
                lab3.text = "出栏时间:\(one.zDate.unwrap)至\(one.zDateEnd.unwrap)"
                lab4.text = one.zPriceRank.unwrap
                chujiaLab.text = "\(intTran(one.zBidCount))次出价"
                
                if  let imageUrl = URL(string:configKSURL(one)) {
                    let resource = KF.ImageResource(downloadURL: imageUrl)
                    imageV.kf.setImage(with: resource, placeholder: UIImage(named:"defaultImg"))
                }
                
                
                if one.innerType! == .jpMyjpKHW {
                    oper_btn.setTitle("查看销售招标", for: .normal)
                    oper_btn.addTarget(self, action: #selector(btn2Action(sender:)), for: .touchUpInside)
                }
                if one.innerType! == .jpInfo_oaOA {
                    oper_btn.setTitle("出价记录", for: .normal)
                    oper_btn.addTarget(self, action: #selector(btn2Action(sender:)), for: .touchUpInside)
                }
                if one.innerType! == .jpInfo_followTagOA {
                    oper_btn.setTitle("出价记录", for: .normal)
                    oper_btn.addTarget(self, action: #selector(btn2Action(sender:)), for: .touchUpInside)
                }

                if oper_btn.titleLabel!.text.unwrap.isEmpty {
                    oper_btn.snp.remakeConstraints { (make) in
                        make.top.equalTo(lab4.snp.bottom).offset(2)
                        make.width.equalTo(1)
                        make.right.equalTo(self.contentView).offset(-15)
                        make.height.equalTo(1)
                        make.bottom.equalTo(self.contentView).offset(-5)
                    }
                }
                
            } else if one.innerType! == .cjAnnouncementKHW {
                
                lab1.attributedText = NSAttributedString(string: "                \(one.zTitle.unwrap)", attributes: titleAttributes)
                lab2.text = "预计上市时间：\(one.zDate.unwrap)"
                lab3.text = "    \(one.zCity.unwrap)    \(one.zBeginDate.unwrap)至\(one.zEndDate.unwrap)"
                let real_status = one.zStatus.isSome ? intTran(one.zStatus) : intTran(one.zZbStatus)
                btn1.setZBJPStatus(listType:.JP,auditmark: intTran(one.zAuditMark), status: real_status,tbType: one.innerType!)
                
 
            } else {
                
                lab1.attributedText = NSAttributedString(string: "                \(one.zTitle.unwrap)", attributes: titleAttributes)
                lab2.text = one.zTitle.unwrap
                lab3.text = "    \(one.zCity.unwrap) \(categoryDicDesc[one.zDtradeType.unwrap].unwrap)\n\(one.zBeginDate.unwrap)至\(one.zEndDate.unwrap)"
                let real_status = one.zStatus.isSome ? intTran(one.zStatus) : intTran(one.zZbStatus)
                btn1.setZBJPStatus(listType:.TB,auditmark: intTran(one.zAuditMark), status: real_status,tbType: one.innerType!)
                
                let re = self.helper.setBtnTitle(listType: one.innerType!, zAuditMark: one.zAuditMark.unwrap, btn2: oper_btn)
                
                if re.isEmpty {
                    oper_btn.snp.remakeConstraints { (make) in
                        make.centerY.equalTo(dingwei)
                        make.width.equalTo(32)
                        make.right.equalTo(self.contentView).offset(-15)
                        make.height.equalTo(32)
                        make.bottom.equalTo(self.contentView).offset(-40)
                    }
                }
                oper_btn.addTarget(self, action: #selector(btn2Action(sender:)), for: .touchUpInside)
                lab4.text = one.zPriceRank.unwrap
            }
            
            
        }
    }
    @objc func btn2Action(sender:UIButton) {
        if let closure = btn2Closure {
            closure(sender,sender.titleLabel!.text.unwrap)
        }
    }
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
